Skip to content

fix(cuda): let the graph-exact Mamba1 scan take float32 A with f16 x - #3128

Merged
inureyes merged 5 commits into
mainfrom
fix/issue-3125-cuda-mamba1-f32-a
Nov 16, 2026
Merged

inureyes merged 5 commits into
mainfrom
fix/issue-3125-cuda-mamba1-f32-a

Conversation

@inureyes

@inureyes inureyes commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

  • CUDA's graph-exact Mamba1 scan now accepts f16 x with the float32 A that f16 Mamba and Jamba keep (f16 cannot hold -exp(A_log)), so f16 Jamba on CUDA takes the kernel instead of the per-timestep graph scan. mamba1_scan_kernel_accepts admits exactly that one extra mix (every other input in f16). Metal and ROCm (float-state port) are untouched: the new TA template arg is added only for the CUDA port.
  • The kernel's new mamba1_decay mirrors what the graph scan does for that pair (decay_in_activation_dtype in jamba.rs, MambaBlock::ssm_step in mamba.rs): MLX multiply promotes delta to float32 and rounds once (__fmul_rn), exp runs on float32 (Exp{}), and astype rounds the decay to f16 (static_cast, as MLX's CastOp does). Uniform-dtype inputs run the same ops as before.
  • ssm_a_f32 and ssm_a_from_log move from mamba.rs into a new models::ssm_a module; Mamba, Jamba, Mamba2, the SSD scan and their tests import from there. No behavior change.
  • jamba_f16_model_with_an_overflowing_a_log_keeps_finite_logits (from fix(models): compute the SSM A from A_log in float32 in six more models #3122) now returns early without a GPU backend: its MoE fixture hits MLX's CPU GatherMM, which only takes float32, and aborted the whole CPU-only lib test process.

Mamba / Falcon-Mamba still take the per-step graph scan on CUDA by design (their gate reads the float-state port; switching would change CUDA output, see MambaBlock::forward). Their f16 scan inputs are now accepted by the predicate, which a test pins, but the Mamba forward path itself is unchanged.

Test plan

  • mamba1_scan (mlxcel-core) and models::mamba, models::jamba, models::ssd_scan pass on Metal, also after merging current main; clippy --lib --tests -D warnings (both crates), fmt, and the env-var, native-env, kernel-port-dispatch and kernel-dtype-key checks pass.
  • New everywhere-run test graph_exact_dtype_rule_admits_only_uniform_inputs_and_f16_with_float32_a fails under both mutations tried (rule widened to any float32 A; rule reverted to uniform only).
  • GB10 (sm_121), --features cuda: mamba1_scan 7 passed, including cuda_kernel_is_bit_identical_to_the_graph_scan_for_f16_x_with_float32_a and the extended cuda_kernel_declines_mixed_dtype_inputs; models::jamba models::mamba models::ssd_scan 56 passed, 3 ignored, including jamba_f16_cuda_kernel_matches_the_graph_scan_bit_for_bit and jamba_mamba_prefill_takes_the_fused_scan_where_a_port_exists. The JIT source compiles under NVRTC.
  • GB10 prefill A/B on ai21-jamba-reasoning-3b-4bit with MLXCEL_CUDA_F16_NORMALIZE=1, 2151-token prompt, 3 runs each (details in the PR comment).
Scan path Prefill (tok/s) Decode (tok/s)
Kernel 2071.8 / 2091.5 / 2115.9 99.4 / 101.1 / 101.2
Graph scan (MLXCEL_MAMBA1_SCAN_KERNEL=0) 1003.3 / 1012.1 / 1058.1 92.3 / 95.1 / 95.8

Prefill is about 2.0x faster and decode about 6% faster. Greedy output (32 tokens) is byte-identical across all six runs and finite. Not measured: an 8192-token prompt.

Closes #3125

f16 Mamba and Jamba keep A = -exp(A_log) in float32 because f16 cannot hold it (#3046, #3057). The CUDA graph-exact scan port refused any input whose dtype differed from x, so f16 Jamba on CUDA (pre-Ampere default, or MLXCEL_CUDA_F16_NORMALIZE on Ampere) fell back to the per-timestep graph scan.

mamba1_scan_kernel_accepts now admits exactly one extra mix on the graph-exact port: f16 x with float32 A, every other input in f16. The launcher passes A in float32 for that pair and names its dtype as a TA template arg (CUDA only; Metal and ROCm sources and cache keys are unchanged). The kernel's new mamba1_decay mirrors the graph scan's three ops for the pair: delta promoted to float32 and multiplied with one rounding (__fmul_rn), exp in float32, then the decay cast to f16 as MLX's CastOp does. Uniform-dtype inputs take the same ops as before.

The dtype rule is also exported on dtype codes (mamba1_graph_exact_scan_accepts_dtypes) so a test checks it on every backend. CUDA-gated tests check bit identity with the graph scan for the mixed pair, the predicate, and that an f16 Jamba takes the kernel with logits equal to the forced graph scan. Mamba's CUDA forward still uses the per-step graph scan by design; a test pins that its f16 scan inputs are accepted.

ssm_a_f32 and ssm_a_from_log move from mamba.rs to a new models::ssm_a module shared by Mamba, Jamba, Mamba2 and the SSD scan, with no behavior change.

No CUDA host was available: the CUDA-gated tests return early on Metal and still need a CUDA run.

Closes #3125
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata arch:ssm State-space / recurrent model (Mamba, RWKV) platform:linux Linux (CUDA / packaging) specific status:done Completed labels Nov 16, 2026
The fixture's feed-forward is MoE, and MLX's CPU GatherMM only accepts float32, so the all-f16 model aborted the whole lib test process on a build without a GPU backend. Return early when no GPU backend is present, matching the neighbouring CUDA-only test.
@inureyes

Copy link
Copy Markdown
Member Author

CUDA verification (GB10, sm_121)

Ran the unchecked items from the test plan on 5b88fac8.

CUDA-only tests: all pass, and all of them actually executed (no early return).

  • cargo test -p mlxcel-core --features cuda --lib mamba1_scan -- --test-threads=1: 7 passed, including cuda_kernel_is_bit_identical_to_the_graph_scan_for_f16_x_with_float32_a, the extended cuda_kernel_declines_mixed_dtype_inputs, and graph_exact_dtype_rule_admits_only_uniform_inputs_and_f16_with_float32_a. The JIT kernel source compiles on CUDA.
  • cargo test -p mlxcel --features cuda --lib -- models::jamba models::mamba models::ssd_scan --test-threads=1: 56 passed, 3 ignored, including jamba_f16_cuda_kernel_matches_the_graph_scan_bit_for_bit and jamba_mamba_prefill_takes_the_fused_scan_where_a_port_exists.

Fix pushed (5b88fac8): without --features cuda (CPU backend), jamba_f16_model_with_an_overflowing_a_log_keeps_finite_logits from #3122 aborted the whole lib test process with [GatherMM::eval] Currently only supports float32.. The fixture's feed-forward is MoE, and MLX's CPU GatherMM rejects f16. The test now returns early when there is no GPU backend, like its CUDA-only neighbour. With the fix, the CPU run passes (56 passed). The CUDA run above still executes the test and it passes.

Prefill throughput: ai21-jamba-reasoning-3b-4bit (bf16 side data, normalized to f16 with MLXCEL_CUDA_F16_NORMALIZE=1). 2151-token prompt, -n 32 --temp 0 --profile, release build, 3 runs each, GPU otherwise idle.

Scan path Prefill (tok/s) Decode (tok/s)
Kernel (MLXCEL_MAMBA1_SCAN_KERNEL=1) 2071.8 / 2091.5 / 2115.9 99.4 / 101.1 / 101.2
Graph scan (MLXCEL_MAMBA1_SCAN_KERNEL=0) 1003.3 / 1012.1 / 1058.1 92.3 / 95.1 / 95.8

Prefill is about 2.0x faster on the kernel, and decode is about 6% faster. The greedy output (32 tokens, reasoning channel) is byte-identical across all six runs and has no NaN or inf. The baseline is the graph scan on this branch, which is the path f16 Jamba took on origin/main before this PR. I did not build origin/main separately.

@inureyes

Copy link
Copy Markdown
Member Author

CUDA verification procedure (GB10)

This PR changes a CUDA kernel that is JIT-compiled at runtime, so nothing about it is verified until it runs on a CUDA device. Please run the steps below on the GB10 host and post the results as a comment on this PR. Do not merge, and do not change kernel code; if a step fails, report the output.

Every command that touches the GPU must run under the host lock: gpu-lock run --tag pr3128 -- <command>. Builds need no lock.

0. Checkout and build

git fetch origin
git checkout fix/issue-3125-cuda-mamba1-f32-a
git pull --ff-only          # head must be 5b88fac8 or later
cargo build --release --features cuda --bin mlxcel --bin mlxcel-bench-decode

--features cuda is required everywhere below. The crates' default feature set is empty, so a build without it is CPU-only: the CUDA tests then return early and report ok in 0.00s, which proves nothing.

1. Correctness tests

gpu-lock run --tag pr3128 -- env RUST_MIN_STACK=16777216 \
  cargo test -p mlxcel-core --features cuda --lib mamba1_scan -- --test-threads=1 --nocapture
gpu-lock run --tag pr3128 -- env RUST_MIN_STACK=16777216 \
  cargo test -p mlxcel --features cuda --lib -- models::jamba models::mamba models::ssd_scan --test-threads=1

Expected: every test passes, and the first command takes clearly longer than 0.00s (NVRTC compiles the kernel). The tests that matter most:

  • cuda_kernel_is_bit_identical_to_the_graph_scan_for_f16_x_with_float32_a (the new f16 x + float32 A path, bit for bit against the graph scan)
  • cuda_kernel_is_bit_identical_to_the_graph_scan (the existing uniform path still matches)
  • cuda_kernel_declines_mixed_dtype_inputs
  • jamba_f16_cuda_kernel_matches_the_graph_scan_bit_for_bit
  • jamba_mamba_prefill_takes_the_fused_scan_where_a_port_exists

A bit-identity test fails rather than skips if the kernel rounds differently from the graph. If NVRTC rejects the source, the failure shows up here as a compile error in the test output; copy it in full.

2. Prefill throughput A/B

Model: models/ai21-jamba-reasoning-3b-4bit (adjust the path if it lives elsewhere). GB10 is sm_121, where bf16 to f16 normalization is off by default, so MLXCEL_CUDA_F16_NORMALIZE=1 is needed to make the activations f16 and reach the new path. Both arms use the same binary; MLXCEL_MAMBA1_SCAN_KERNEL=0 is the graph-scan baseline.

M=models/ai21-jamba-reasoning-3b-4bit
for len in 2048 8192; do
  for round in 1 2; do
    for arm in kernel graph; do
      K=1; [ "$arm" = graph ] && K=0
      gpu-lock run --tag pr3128 -- env MLXCEL_CUDA_F16_NORMALIZE=1 MLXCEL_MAMBA1_SCAN_KERNEL=$K \
        ./scripts/bench_decode.sh "$M" --prompt-tokens $len --max-tokens 32 \
        --output benchmarks/pr3128-jamba-f16-$arm-$len-r$round.csv
    done
  done
done

Report a table of prefill_tok_s and decode_tok_s per arm, length and round from the eight CSVs, plus mlx_commit and uptime load average at the start. Keep the host otherwise idle.

3. Output equality

for K in 1 0; do
  gpu-lock run --tag pr3128 -- env MLXCEL_CUDA_F16_NORMALIZE=1 MLXCEL_MAMBA1_SCAN_KERNEL=$K \
    ./target/release/mlxcel generate -m "$M" \
    -p "Explain the Mamba selective scan in three sentences." -n 64 --temp 0 > /tmp/pr3128-out-$K.txt 2>&1
done
diff <(grep -v '^\[Generated' /tmp/pr3128-out-1.txt) <(grep -v '^\[Generated' /tmp/pr3128-out-0.txt) && echo IDENTICAL

Expected: IDENTICAL, and the text is coherent and finite. Paste the kernel-arm output.

4. New MLX pin compile check (separate from this PR)

main now pins MLX fc344c98 (#3134), whose only change is in the CUDA build (ml-explore/mlx#4839), and no CUDA host has compiled it yet. After steps 1 to 3:

git checkout origin/main
cargo build --release --features cuda --bin mlxcel
gpu-lock run --tag pr3128 -- ./target/release/mlxcel generate -m "$M" -p "Hello" -n 16 --temp 0

Report whether the build succeeds and the binary generates. Then check this PR's branch out again if anything needs re-running.

What to post back

One comment with: host, driver and CUDA version, the head commit tested, step 1 test results (pass/fail lines and timing), the step 2 table, the step 3 verdict and output, and the step 4 result.

@inureyes
inureyes marked this pull request as ready for review November 16, 2026 16:26
@inureyes
inureyes merged commit ce8de83 into main Nov 16, 2026
32 checks passed
@inureyes
inureyes deleted the fix/issue-3125-cuda-mamba1-f32-a branch November 16, 2026 16:46
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:ssm State-space / recurrent model (Mamba, RWKV) area:models Model architectures, weights, loading, metadata platform:linux Linux (CUDA / packaging) specific priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Let CUDA Mamba1 scan take float32 A with f16 x; share SSM A helpers

1 participant